import axios from 'axios'

axios.defaults.timeout = 60000
axios.defaults.withCredentials = true

// 认证类型
const AUTH_TYPE = {
  BEARER: 'Bearer',
  BASIC: 'basic',
  AUTH1: 'auth1',
  AUTH2: 'auth2'
}

// http method
const METHOD = {
  GET: 'get',
  POST: 'post',
  PUT: 'put',
  DELETE: 'delete'
}

/**
 * axios请求
 * @param url 请求地址
 * @param method {METHOD} http method
 * @param params 请求参数
 * @returns {Promise<AxiosResponse<T>>}
 */
async function request (url, method, params, config) {
  switch (method) {
    case METHOD.GET:
      return axios.get(url, { params, ...config })
    case METHOD.POST:
      if (params instanceof FormData) {
        params.append('version', '1.0')
        return axios.post(url, params, config)
      } else {
        return axios.post(
          url,
          { ...params },
          config
        )
      }
    case METHOD.DELETE:
      return axios.delete(url, { params, ...config })
    case METHOD.PUT:
      // return axios.put(url, params, config)
      return axios.put(url, { request: params }, config)
    default:
      return axios.get(url, { params, ...config })
  }
}

export {
  METHOD,
  AUTH_TYPE,
  request
}
